var referee_value=0;
var strlSocketIO={
    _firstLogin:false,
    _initConnect:function(){
        console.log('正在建立连接...');
        try{
            socket  =  new WebSocket('wss://catchtoy.mxzhuawawa.com:9979');
        }catch(e){
            console.log('连接异常 ： '+e);
            return;
        }
        strlSocketIO._wbSocket = socket;
        socket.onopen = function(event) {
            if(!strlSocketIO._firstLogin){
                if(isLogin) {
                    var data={};
                    data.cmd = "login";
                    data.uid = user.uid;
                    data.machine_id =machine_id;
                    data.sign =token;
                    strlSocketIO._sendMsg(JSON.stringify(data));
                }
            }

            socket.onmessage = strlSocketIO._msgReceive;

            socket.onclose = function(event) {
                console.log('连接关闭.',event);
            };
        };
    },
    _sendMsg:function(msgBuf){
        if(msgBuf!=null&&msgBuf!='undefined'){
            strlSocketIO._wbSocket.send(msgBuf);
        }else{
            console.log('发送消息为空!');
        }
    },

    _msgReceive:function(event) {
        var data = JSON.parse(event.data);
        console.log(data);
        if(zhuawawa.hasOwnProperty('_'+data.cmd)){
            eval('zhuawawa._'+ data.cmd  + '(data)');
        }
    }
}



var zhuawawa={
    coinbalance:0,
    times:null,
    _game_state:function(data){
        $(".gamestarting").addClass("none");
        if(data.data == "0"){
            $(".show_ctrls .ctrlstatr").addClass("dstartBg");
        }else{
            $(".show_ctrls .ctrlstatr").removeClass("dstartBg");
        }
    },
    _game_result:function(data){
        clearInterval(this.times);
        $(".downtime").html("")
        var html=null;
        if(this.coinbalance<_num_){
            if(data.data=="0"){
                html='<div class="sysmsgcontent"><button class="result_close"></button><div class="sysimg"><img style="margin-top:-20px;" src="src/img/nono.png?v=1"><p class="bb">别灰心啦，小梦发现您只有'+this.coinbalance+'金币<br>已经不能抓啦，赶紧去充值吧！</p><p class="info">邀请好友您将获得'+referee_value+'金币的奖励哦</p></div><div class="sysbutton"><a href="/invitation.html" class="btn invitation">邀请好友</a> <a href="/recharge.html" class="btn once recharge">立即充值</a></div></div>'
            }else{
                html='<div class="sysmsgcontent" style="height:380px;"><button class="result_close"></button><div class="sysimg"><img style="width:50%;margin-bottom:20px;" src="src/img/zhuazhongle.png?v=1"><p class="bb">恭喜您抓住啦一个“'+toy.title+'”<br>小梦发现您只有'+this.coinbalance+'金币<br>已经不能抓啦，赶紧去充值吧！</p><p class="info">感觉把您的喜悦分享给您的好友啦<br>让他们来跟您一起享受抓娃娃的乐趣吧！</p></div><div class="sysbutton"><a href="/invitation.html" class="btn invitation">立即分享</a> <a href="/recharge.html" class="btn once recharge">立即充值</a></div></div>'
            }
        }else{
            if(data.data=="0"){
                html='<div class="sysmsgcontent"><button class="result_close"></button><div class="sysimg"><img style="margin-top:-20px;" src="src/img/nono.png?v=1"><p class="bb">别灰心啦，小梦发现您还有'+this.coinbalance+'金币<br>还可以再抓取哦！</p><p class="info">邀请好友您将获得'+referee_value+'金币的奖励哦</p></div><div class="sysbutton"><a href="/invitation.html" class="btn invitation">邀请好友</a> <button class="btn once start">立即重试</button></div></div>'
            }else{
                html='<div class="sysmsgcontent"><button class="result_close"></button><div class="sysimg"><img  style="width:50%;margin-bottom:20px;" src="src/img/zhuazhongle.png?v=1"><p class="bb">恭喜您抓住啦一个“'+toy.title+'”<br>小梦已经给您放到您的背包啦！</p><p class="info">感觉把您的喜悦分享给您的好友啦<br>让他们来跟您一起享受抓娃娃的乐趣吧！</p></div><div class="sysbutton"><a href="/invitation.html" class="btn invitation">立即分享</a> <button class="btn once start">再抓一次</button></div></div>'
            }
        }
        $(".sysmsgalert").removeClass("none").html(html);
        $(".show_ctrlsed").addClass("none");
        $(".show_ctrls").removeClass("none");
        
        btnflag=false;
    },
    _alert:function(data){
        $(".gamestarting").addClass("none");
        $(".topalert").html(data.data).addClass("animate");
        setTimeout(function(){
            $(".topalert").removeClass("animate");
        },2000)
        if(data.code==2){
            $(".gorecharge").removeClass("none");
        }
    },
    _start_game:function(data){
        this.coinbalance=parseInt(data.coinbalance);
        $(".gamestarting").addClass("none");
        var t=30;
        var _this=this;
        this.times=setInterval(function(){
            t--;
            $(".downtime").html(t+"'")
            if(t==0){
                t=30;
                $(".downtime").html("") 
                clearInterval(_this.times);
            }
        },1000)
        this.coinbalance=parseInt(data.coinbalance);
        $(".mymoney").html(parseInt(data.coinbalance));

        cookieSet("coinbalance",parseInt(data.coinbalance));

        if(btnflag){
            $(".show_ctrls").addClass("none");
            $(".show_ctrlsed").removeClass("none");
        }
    },
    _user_profile:function(data){
        $(".mymoney").html(parseInt(data.data.coinbalance));
        cookieSet("coinbalance",parseInt(data.data.coinbalance));
    }
}

if(token){
    fetchData("/OpenAPI/v1/app/referee",{"token":token},function(data){
        referee_value=data.referee_value;
    });
}


